﻿// -----------------------------------------------------------------------
// <copyright file="Quantization.cs" company="FCIS">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace IPPackage.ImageOperation
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Quantization : Operation
    {
        /// <summary>
        /// Quantizations the image.
        /// </summary>
        /// <param name="mask">The mask.</param>
        /// <returns>The new image</returns>
        public Bitmap QuantizationImg(int mask)
        {
            this.NewBufferOfImage = new Color[this.BufferOfImage.GetLength(0), this.BufferOfImage.GetLength(1)];
            Bitmap newimg = new Bitmap(this.BufferOfImage.GetLength(0), this.BufferOfImage.GetLength(1));
            int width = this.BufferOfImage.GetLength(0);
            int hight = this.BufferOfImage.GetLength(1);
            Color c = new Color();
            for (int i = 0; i < hight; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    byte red = (byte)(Convert.ToInt32(this.BufferOfImage[j, i].R) & mask);
                    byte green = (byte)(Convert.ToInt32(this.BufferOfImage[j, i].G) & mask);
                    byte blue = (byte)(Convert.ToInt32(this.BufferOfImage[j, i].B) & mask);
                    byte alpa = (byte)(Convert.ToInt32(this.BufferOfImage[j, i].A) & mask);
                    this.NewBufferOfImage[j, i] = Color.FromArgb(alpa, red, green, blue);
                    c = Color.FromArgb(alpa, red, green, blue);
                    newimg.SetPixel(j, i, c);
                }
            }

            return newimg;
        }
    }
}
